package com.hahehiho.myalbum;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


public class Indexer {
	
	private File file;
	private Map<String, MyAlbumVO> items;

	public Indexer(File file) {
		this.file = file;
		this.items = new LinkedHashMap<String, MyAlbumVO>();
	}

	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		
		Indexer indexer = new Indexer(new File("C:\\Users\\hahehiho\\Pictures"));
		indexer.createIndex();
		
		long endTime = System.currentTimeMillis();
		
		System.out.println(indexer);
		
		long eltime = endTime - startTime;
		
		System.out.println("total time : " + eltime/1000 + "sec " + eltime%1000 + "msec"); 
	}

	public void createIndex() {
		createIndex(file);
	}

	private void createIndex(File file) {
		if(file.isDirectory()) {
			File[] files = file.listFiles();
			for (File file2 : files) {
				createIndex(file2);
			}
		} else {
			MyAlbumVO metadata = ImageFileHandler.readMyAlbumVO(file);
			items.put(file.getAbsolutePath(), metadata);
			if(items.size()%100 == 0)
				System.out.println("working metadata size: " + items.size());
		}
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		
		Iterator<String> it = items.keySet().iterator();
		while(it.hasNext()) {
			String filePath = it.next();
			sb.append(filePath + "\n");
			sb.append(items.get(filePath));
		}
		
		return sb.toString();
	}

	public Map<String, MyAlbumVO> getItems() {
		return items;
	}

}
